import React from "react";
import {HomeWrapper} from "@/component/Home/style";
import {getQueryVariable, warnMsg} from "@/util/global";
import {getGameDetail} from "@/api/JustGameInfo";
import NoDataWindow from "@/common/window/NoDataWindow";
import BraftEditor from "braft-editor";
import Header from "./Header";
import Body from './Body';
import {getScore, pushScore} from "@/api/JustGameScore";
import {fromBase64} from "@/util/CodeUtils";

/**
 * 游戏详情展示界面
 */
class GameContainer extends React.Component {
    constructor(props) {
        super(props);
        this.pushScore = this.pushScore.bind(this);
        this.changeScore = this.changeScore.bind(this);
        this.loadData = this.loadData.bind(this);
        this.state = {
            cnName: "",
            companyImage: "",
            companyName: "",
            enName: "",
            id: "",
            images: "",
            platformList: [],
            time: "",
            typeList: [],
            platformKeys: [],
            photos: [],
            videoUrl: '',
            loadData: true,
            score: 0,
            rateCount: 0,
            description: BraftEditor.createEditorState(null).toHTML(),
            isScore: 1,
        };

    }


    componentDidMount() {
        /* 没有传入特定的id，视为直接访问url跳转到该页面，遣返 */
        let id = null;
        if (this.props.location.query && this.props.location.query.id) {
            id = this.props.location.query.id
        } else if (window.location.search) {
            id = fromBase64(getQueryVariable('id'));
        } else {
            warnMsg('无法直接访问该页面哦！')
            window._ROUTER_.push('/home');
            return;
        }
        this.setState({id}, this.loadData)
    }

    loadData() {
        getGameDetail(this.state.id).then(
            response => {
                if (response.success) {
                    let platformKeys = [];
                    if (response.data.platformList)
                        platformKeys = Array.from(new Set(response.data.platformList.map(item => {
                            return item.name
                        })));

                    this.setState({
                        ...response.data,
                        platformKeys,
                        description: BraftEditor.createEditorState(response.data.description).toHTML(),
                        loadData: false
                    })
                }
            }
        )
    }

    pushScore(score) {
        let data = {
            gameId: this.state.id,
            score: this.state.score
        }
        pushScore(data).then(res => {
            if (res.success) {
                getScore(this.state.id).then(res => {
                    this.setState({
                        score: res.data.score,
                        rateCount: res.data.rateCount,
                        isScore: 1
                    })
                })
            }
        })
    }

    changeScore(e) {
        e = e === 0 ? 5 : e;
        let data = {
            gameId: this.state.id,
            score: e
        }
        new Promise((resolve, reject) => {
            pushScore(data).then(res => {
                if (res.success) {
                    getScore(this.state.id).then(res => {
                        resolve(res)
                    })
                }
            })
        }).then(res => {
            this.setState({
                score: res.data.score,
                rateCount: res.data.rateCount,
                isScore: 1,
            })
        })
    }


    render() {
        return <HomeWrapper>
            {
                !this.state.loadData ?
                    <>
                        <Header data={this.state}
                                pushScore={this.pushScore}
                                changeScore={this.changeScore}
                                reload={this.loadData}
                        />
                        <Body data={this.state}/>
                    </> : <NoDataWindow/>
            }
        </HomeWrapper>
    }
}

export default GameContainer;
